package sealte.mynode.servlet.impl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.mynode.bean.EssayBean;
import sealte.mynode.pojo.Essay;
import sealte.mynode.servlet.system.SystemPostServlet;
import sealte.mynode.util.Tools;

public class EssayAddNew extends SystemPostServlet {

	private static final long serialVersionUID = -2905861491818344565L;
	
	@Override
	protected void doBusinessService(HttpServletRequest req,HttpServletResponse resp)
			throws ServletException, IOException{
		PrintWriter out = resp.getWriter();
		String authorStr = (String)req.getParameter("author");
		String title = (String)req.getParameter("title");
		String content = (String)req.getParameter("content");
		int author = Integer.parseInt(authorStr);
		String date = Tools.getCurrentDate();
		
		Essay essay = new Essay();
		essay.setAuthorId(author);
		essay.setWriteTime(date);
		essay.setTitle(title);
		essay.setContent(Tools.formatContent(content));
		essay.setModifyTime(date);
		
		EssayBean eb = new EssayBean();
		boolean flag = eb.addNew(essay);
		if(flag){
			resp.sendRedirect("home.jsp");
		}else{
			out.println("Essay add new failed!");
			out.println("<a href=\"essay_addnew.jsp\">Add new essay again</a>");
		}
		
		if(null!=out){
			out.close();
		}
	}

}
